package com.itms.tool.data;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

	private static final String DATABASE_NAME = "itms_tool.db";
	private static final int DATABASE_VERSION = 4;
	
	private RuntimeExceptionDao<Category, Integer> categoryDao;
	private RuntimeExceptionDao<Image, Integer> imageDao;
	private RuntimeExceptionDao<Object, Integer> objectDao;
	private RuntimeExceptionDao<ObjectTopic, Integer> objectTopicDao;
	private RuntimeExceptionDao<Topic, Integer> topicDao;
	
	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		
	}

	@Override
	public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
		try {
			TableUtils.createTable(connectionSource, Category.class);
			TableUtils.createTable(connectionSource, Image.class);
			TableUtils.createTable(connectionSource, Object.class);
			TableUtils.createTable(connectionSource, ObjectTopic.class);
			TableUtils.createTable(connectionSource, Topic.class);
			populateMockData();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
		try {
			TableUtils.dropTable(connectionSource, Category.class, true);
			TableUtils.dropTable(connectionSource, Image.class, true);
			TableUtils.dropTable(connectionSource, Object.class, true);
			TableUtils.dropTable(connectionSource, ObjectTopic.class, true);
			TableUtils.dropTable(connectionSource, Topic.class, true);
			onCreate(database, connectionSource);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	
	private void populateMockData() {
		int nTopics = 10;
		int nParentCategories = 7;
		for (int i = 0; i < nTopics; i++) {
			Topic topic = new Topic();
			topic.setId(i + 1);
			topic.setName("Topic - " + (i + 1));
			getTopicDao().create(topic);
		}
		for (int i = 0; i < nParentCategories; i++) {
			int nSubCategories = (int) (Math.random() * 3 + 5); 
			Category parentCategory = new Category();
			parentCategory.setId(i + 1);
			parentCategory.setName("ParentCategory - " + (i + 1));
			getCategoryDao().create(parentCategory);
			for (int j = 0; j < nSubCategories; j++) {
				Category childCategory = new Category();
				childCategory.setId(nParentCategories * (i + 1) + (j + 1));
				childCategory.setName("ChildCategory - " + (i + 1) + "." + (j + 1));
				childCategory.setParentCategory(parentCategory);
				getCategoryDao().create(childCategory);
			}
		}
	}
	
	public RuntimeExceptionDao<Category, Integer> getCategoryDao() {
		if (categoryDao == null) {
			categoryDao = getRuntimeExceptionDao(Category.class);
		}
		return categoryDao;
	}
	
	public RuntimeExceptionDao<Image, Integer> getImageDao() {
		if (imageDao == null) {
			imageDao = getRuntimeExceptionDao(Image.class);
		}
		return imageDao;
	}
	
	public RuntimeExceptionDao<Object, Integer> getObjectDao() {
		if (objectDao == null) {
			objectDao = getRuntimeExceptionDao(Object.class);
		}
		return objectDao;
	}
	
	public RuntimeExceptionDao<ObjectTopic, Integer> getObjectTopicDao() {
		if (objectTopicDao == null) {
			objectTopicDao = getRuntimeExceptionDao(ObjectTopic.class);
		}
		return objectTopicDao;
	}
	
	public RuntimeExceptionDao<Topic, Integer> getTopicDao() {
		if (topicDao == null) {
			topicDao = getRuntimeExceptionDao(Topic.class);
		}
		return topicDao;
	}

}